import os
import win32com.client

ppt_app = win32com.client.Dispatch("PowerPoint.Application")
ppt_app.Visible = True  # 设置 PowerPoint 可见

def split_ppt(presentation):
    # output_folder = 'output_images'  # 图片输出文件夹


    for slide_index in range(1, presentation.Slides.Count + 1):
        filename = os.path.basename(ppt_file_path)
        image_path = os.path.abspath(os.path.join(IMG_STORE_PATH, f'{filename}--page{slide_index}.jpg'))
        print(image_path)
        if os.path.exists(image_path):  # 如果图片已经存在，则跳过
            continue
        slide = presentation.Slides(slide_index)
        
        print(slide)
        slide.Export(image_path, 'JPG')  # 将幻灯片导出为 JPG 格式

PPT_STORE_PATH = r'C:\Users\houzh\Documents\WPSDrive\312506692\WPS云盘\研究生\历史文档汇总收集\PPT'
IMG_STORE_PATH = r'C:\Users\houzh\Documents\WPSDrive\312506692\WPS云盘\研究生\历史文档汇总收集\PPT\imgs'

if not os.path.exists(IMG_STORE_PATH):
    os.makedirs(IMG_STORE_PATH)

# ppt_file_path = r'C:\Users\houzh\Documents\WPSDrive\312506692\WPS云盘\研究生\历史文档汇总收集\PPT\code-to-uppaal.pptx'  # PPT 文件路径
# presentation = ppt_app.Presentations.Open(ppt_file_path)

for file in os.listdir(PPT_STORE_PATH):
    if file.endswith(('.pptx', ".ppt")) and not file.startswith('~$'):
        ppt_file_path = os.path.join(PPT_STORE_PATH, file)
        print("trying to open presentation", ppt_file_path)
        presentation = ppt_app.Presentations.Open(ppt_file_path)
        split_ppt(presentation)
# split_ppt(presentation)
        presentation.Close()  # 关闭 PPT 文件

ppt_app.Quit()       # 退出 PowerPoint 应用程序